package com.bkfm.framework.wf.rest;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yak.core.web.BaseRest;
import org.yak.core.web.Result;
import org.yak.core.web.Result.Status;

import com.bkfm.framework.wf.api.service.IWorkFlowService;
import com.bkfm.framework.wf.entity.BkfmWorkFlow;
import com.bkfm.framework.wf.entity.request.BkfmWorkFlowRequestInfo;


/**
 * 工作流接口框架
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/wf/v1/**")
public class WorkFlowRest extends BaseRest<BkfmWorkFlow>{
	/**
	 * 部署流程
	 */
	public static final String DEPLOY_WORKFLOW = "deploy";
	
	/**
	 * 更新部署流程
	 */
	public static final String UPDATE_DEPLOY_WORKFLOW = "update_deploy";

	@Resource
	private IWorkFlowService workFlowService;
	
	/**
	 * 查询流程图信息
	 * @param bkfmWorkFlow
	 * @return
	 */
	@RequestMapping("/queryworkflow")
	public Result queryWorkFlow(BkfmWorkFlow bkfmWorkFlow) {
		try {
			bkfmWorkFlow = workFlowService.queryWorkFlow(bkfmWorkFlow);
			return new Result(Status.OK,"success",bkfmWorkFlow);
		}catch(Exception e) {
			logger.error("获取流程图信息异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	/**
	 * 
	 * @param workFlow 流程图对象
	 * @param workFlowConfig 流程图解析文件
	 * @return
	 */
	@PostMapping("/save")
	public Result saveWorkFlow(@RequestBody BkfmWorkFlow bkfmWorkFlow) {
		try {
			workFlowService.saveWorkFlow(bkfmWorkFlow);
			return new Result(Status.OK,"success");
		}catch(Exception e) {
			logger.error("保存流程异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	/**
	 * 部署流程
	 * @param bkfmWorkFlow
	 * @param deploy_style 部署方式
	 * @return
	 */
	@RequestMapping("/deployworkflow")
	public Result deployWorkFlow(BkfmWorkFlow bkfmWorkFlow,String deploy_style) {
		try{
			if(DEPLOY_WORKFLOW.equals(deploy_style)) {
				workFlowService.deployWorkFlow(bkfmWorkFlow);
			}else if(UPDATE_DEPLOY_WORKFLOW.equals(deploy_style)) {
				workFlowService.updateDeployWorkFlow(bkfmWorkFlow);
			}else {
				return new Result(Status.ERROR,"无效的部署方式");
			}
			return new Result(Status.OK,"success");
		}catch(Exception e) {
			logger.error("部署流程异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
	
	
	/**
	 * 流程启动，启动成功后返回流程的BkfmWorkFlowRequestInfo详细信息
	 * @param workFlowRequestInfo
	 * @return
	 */
	@RequestMapping("/startworkflow")
	public Result startWorkFlow(BkfmWorkFlowRequestInfo workFlowRequestInfo) {
		try {
			
			return new Result(Status.OK,"success",workFlowRequestInfo);
		}catch(Exception e) {
			logger.error("启动流程异常",e);
			return new Result(Status.ERROR,e.getMessage());
		}
	}
}
